import configparser
import os
#root_dir = os.path.dirname(os.path.abspath('.'))  # 获取当前文件所在目录的上一级目录，即项目所在目录E:\Crawler
from util import hostUtil, jsonUtil

root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
cf = configparser.ConfigParser()
cf.read(root_dir+"/config.ini")  # 拼接得到config.ini文件的路径，直接使用
class configUtil:
    '''
    获取当前环境并返回当前环境配置路径
    '''
    def __index__(self):
        pass



    def getEnvDir(self):
        Base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        print("Base_dir is ", Base_dir)
        #root_dir = os.path.dirname(os.path.abspath('.'))  # 获取当前文件所在目录的上一级目录，即项目所在目录E:\Crawler
        #print("root dir is ",root_dir)
        cf = configparser.ConfigParser()
        cf.read(Base_dir + "/config.ini")  # 拼接得到config.ini文件的路径，直接使用
        env = cf.get("active", "env")
        print(env)
        if hostUtil.personalPc() == 1:
            env = "local-dataDir"
        return cf.get(env, "dataDir")
    def getModel(self):
        Base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        cf = configparser.ConfigParser()
        cf.read(Base_dir + "/config.ini")  # 拼接得到config.ini文件的路径，直接使用
        env = cf.get("active", "env")
        if hostUtil.personalPc() == 1:
            env = "local-dataDir"
        return cf.get(env, "model_dir")
    def getpersonalModel(self):
        Base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        cf = configparser.ConfigParser()
        cf.read(Base_dir + "/config.ini")  # 拼接得到config.ini文件的路径，直接使用
        env = "local-dataDir"
        return cf.get(env, "model_dir")
    def getFaceProcessConfigCommon(self):
        return {'searchScoreThreshold':0.4,
                'bodyKeyPointInputHeight':576}
    def getVideoPath(self):
        josn = jsonUtil.readConfigJosn()
        inintdir = josn['localPersistentBaseDir']
        remote = self.getPersistentDirs('/media/oss/')
        if hostUtil.personalPc() == 1:
            inintdir = 'C:\\test\\video'
        persistentMode = josn['persistentMode']
        print('persistentMode is ', persistentMode)
        local = self.getPersistentDirs(inintdir)
        persistentDirConfigs = {'local': local, 'remote': remote, 'current': None}
        if persistentMode == 'local':
            persistentDirConfigs['current'] = persistentDirConfigs['local']
        else:
            persistentDirConfigs['current'] = persistentDirConfigs['remote']
        # 加载配置
        sliceDir = persistentDirConfigs['current']['video']
        return sliceDir

    # 获取路径配置
    def getPersistentDirs(self,persistentBaseDir):
        _private = os.path.join(persistentBaseDir, 'private/')
        _static = os.path.join(persistentBaseDir, 'static/')
        archive = os.path.join(persistentBaseDir, 'archive')
        temp = os.path.join(persistentBaseDir, 'temp/')
        configMap = {}
        configMap['video'] = os.path.join(_private, 'record-video')
        configMap['archiveVideo'] = os.path.join(archive, 'record-video')
        configMap['faceResult'] = os.path.join(_private, 'face-process')
        configMap['frame'] = os.path.join(temp, 'video-frame')
        configMap['childSnapshotExt'] = os.path.join(temp, 'child-snapshot-extended')
        configMap['childSnapshot'] = os.path.join(temp, 'child-snapshot')
        # 人体关键点检测
        configMap['bodyKeyPointResult'] = os.path.join(temp, 'body-key-point-result')
        # 用于人体关键点检测的输入数据目录
        configMap['bodyKeyPointInput'] = os.path.join(temp, 'body-key-point-input')
        # 用于人体关键点检测的过程控制目录
        configMap['bodyKeyPointProcessControl'] = os.path.join(temp, 'body-key-point-process-control')

        return configMap
    def getcurrentClass(self,typestr):
        Base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        cf = configparser.ConfigParser()
        cf.read(Base_dir + "/config.ini")  # 拼接得到config.ini文件的路径，直接使用
        env = cf.get("active", "env")
        if hostUtil.personalPc() == 1:
            env = "local-dataDir"
        return cf.get(env, typestr)

#secs = cf.sections()  # 获取文件中所有的section(一个配置文件中可以有多个配置，如数据库相关的配置，邮箱相关的配置，每个section由[]包裹，即[section])，并以列表的形式返回
#print(secs)

#options = cf.options("pro-db")  # 获取某个section名为Mysql-Database所对应的键
#print(options)

#items = cf.items("test-db")  # 获取section名为Mysql-Database所对应的全部键值对
#print(items)

#host = cf.get("test-db", "host")  # 获取[Mysql-Database]中host对应的值
#print(host)



if __name__ == '__main__':
    configUtil = configUtil()
    print(configUtil.getEnvDir())